using UnityEngine;

namespace NodeCanvas.BehaviourTrees
{
	[AddComponentMenu("")]
	[Name("Condition")]
	[Description("Check a condition and return Success or Failure")]
	[Icon("Condition")]
	public class BTCondition : BTNodeBase, ITaskAssignable<ConditionTask>, ITaskAssignable
	{
		[SerializeField]
		private Object _condition;

		[SerializeField]
		private BTCondition _referencedNode;

		public Task task
		{
			get
			{
				return condition;
			}
			set
			{
				condition = (ConditionTask)value;
			}
		}

		public Object serializedTask
		{
			get
			{
				return _condition;
			}
		}

		private ConditionTask condition
		{
			get
			{
				if (referencedNode != null)
				{
					return referencedNode.condition;
				}
				return _condition as ConditionTask;
			}
			set
			{
				if (referencedNode != null)
				{
					referencedNode.condition = value;
				}
				else
				{
					_condition = value;
				}
				if (value != null)
				{
					value.SetOwnerSystem(base.graph);
				}
			}
		}

		public BTCondition referencedNode
		{
			get
			{
				return _referencedNode;
			}
			private set
			{
				_referencedNode = value;
			}
		}

		public override string nodeName
		{
			get
			{
				return base.nodeName.ToUpper();
			}
		}

		protected override Status OnExecute(Component agent, Blackboard blackboard)
		{
			if ((bool)condition)
			{
				return condition.CheckCondition(agent, blackboard) ? Status.Success : Status.Failure;
			}
			return Status.Failure;
		}
	}
}
